package com.pic4493.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pic4493.base.BaseUITopBar;
import com.pic4493.updater.TaskCheckVersion;
import com.pic4493.utils.UtiDialog;
import com.pic4493.utils.UtiFile;
import com.ppcodes.imageloader.core.ImageLoader;
import com.ppcodes.imageloader.utils.StorageUtils;

public class UISetting extends BaseUITopBar implements OnClickListener
{

   private LinearLayout mLayAdvise;
   private LinearLayout mLayAbout;
   private LinearLayout mLayUpdate;

   private LinearLayout mLayHDImg;
   private ImageView mImgHD;
   private LinearLayout mLayCache;
   private LinearLayout mLayWallPaper;
   
   private TextView mTxtCache;

   private boolean isHD;

   private void initView()
   {
	  mLayAdvise = (LinearLayout) findViewById(R.id.uisetting_lay_advise);
	  mLayAbout = (LinearLayout) findViewById(R.id.uisetting_lay_about);
	  mLayUpdate = (LinearLayout) findViewById(R.id.uisetting_lay_update);

	  mLayHDImg = (LinearLayout) findViewById(R.id.uisetting_lay_hdimg);
	  mImgHD = (ImageView) findViewById(R.id.uisetting_img_hd);
	  mLayCache = (LinearLayout) findViewById(R.id.uisetting_lay_cache);
	  mTxtCache = (TextView) findViewById(R.id.uisetting_txt_cache);
	  mTxtCache.setText("本地缓存共有" + getCacheSize() + "m");
	  mLayWallPaper=(LinearLayout)findViewById(R.id.uisetting_lay_wallpaper);

   }

   private String getCacheSize()
   {
	  return String.valueOf(UtiFile.getFolderSize(StorageUtils.getCacheDirectory(mContext)));
   }

   private void initEvent()
   {
	  mLayAbout.setOnClickListener(this);
	  mLayAdvise.setOnClickListener(this);
	  mLayUpdate.setOnClickListener(this);
	  mLayCache.setOnClickListener(this);
	  mLayHDImg.setOnClickListener(this);
	  mLayWallPaper.setOnClickListener(this);
   }

   private void setHDImage()
   {
	  isHD = !isHD;
	  if (isHD)
	  {
		 mImgHD.setImageResource(R.drawable.uisetting_hdimg_yes);
	  }
	  else
	  {
		 mImgHD.setImageResource(R.drawable.uisetting_hdimg_no);
	  }

   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  setContentView(R.layout.uisetting);
	  super.onCreate(savedInstanceState);
	  setTitle("设置");

	  initView();
	  initEvent();
   }

   @Override
   public void onClick(View v)
   {
	  Intent uiIntent = new Intent();
	  switch (v.getId())
	  {
		 case R.id.uisetting_lay_about:
			uiIntent.setClass(UISetting.this, UIAbout.class);
			startActivity(uiIntent);
			break;
		 case R.id.uisetting_lay_advise:
			uiIntent.setClass(UISetting.this, UIAdvise.class);
			startActivity(uiIntent);
			break;
		 case R.id.uisetting_lay_update:
            TaskCheckVersion taskCheckVersion=new TaskCheckVersion(mContext, true);
            taskCheckVersion.execute();
			break;
		 case R.id.uisetting_lay_hdimg:

			setHDImage();
			break;
		 case R.id.uisetting_lay_cache:
			UtiDialog.getInstance().ShowOKCancelAlertDialog(mContext, "注意", "会清除SD卡中的所有缓存,确定吗?", new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  executeAsyncTask("cleanCache");
			   }
			});
			break;
		 case R.id.uisetting_lay_wallpaper:
			uiIntent.setClass(mContext, UIWallPaper.class);
			startActivity(uiIntent);
			break;

	  }
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK)
	  {
		 finish();
	  }
	  return true;
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialog.getInstance().ShowLoadingDialogNoTitle(mContext, "请稍候...");
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  ImageLoader.getInstance().clearDiscCache();
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  UtiDialog.getInstance().DismissLoadingDialog();
	  mTxtCache.setText("本地缓存共有" + getCacheSize() + "m");
   }
}
